package com.huangliusong.desigin.pattern.creational.singleton;

import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author huangliusong
 * @since 2018/10/10
 * {@link }
 */
public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
      //  LazySingleton lazySingleton=LazySingleton.getInstance();
//        Thread t1=new Thread(new T());
//        Thread t2=new Thread(new T());
//        t1.start();
//        t2.start();
//         System.err.println("end");

//
//        HungrySingleton instance=HungrySingleton.getInstance();
//        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("file.txt"));
//        oos.writeObject(instance);
//
//        File file=new File("file");
//        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
//
//        HungrySingleton newInstance= (HungrySingleton) ois.readObject();
//
//
//        System.out.println(instance);
//        System.out.println(newInstance);
//        System.err.println(newInstance==instance);

        Class objClass=LazySingleton.class;
        //Class.forName(LazySingleton.class.getName());

        Constructor constructor=objClass.getDeclaredConstructor();
        //通过反射吧权限变成true
        constructor.setAccessible(true);

        LazySingleton instance=LazySingleton.getInstance();
        LazySingleton newInstance= (LazySingleton) constructor.newInstance();
        //HungrySingleton instance=HungrySingleton.getInstance();
       // HungrySingleton obj= (HungrySingleton) constructor.newInstance();
        System.out.println(instance);
        System.out.println(newInstance);
        System.err.println(instance==newInstance);

    }

}
